package com.DAO;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;


import com.beans.*;

import com.connection.ConnectionFactory;
public class BirthdayDAO {

private Connection con=null;
	
	public BirthdayDAO()
	{
		
		con = ConnectionFactory.getConnection();
		
	}

	
	
	public ArrayList<BirthdayForm> showBirthdays() throws SQLException
	{
		String query = "SELECT U.*FROM Users U where month(SYSDATE()) =  month(U.birth_date) AND day(SYSDATE())< day(U.birth_date)";
		Statement st = null;
		
		ResultSet rs = null;
		
		ArrayList<BirthdayForm> list=new ArrayList<BirthdayForm>();
		
		try 
		{
			
			st = con.createStatement();
			rs = st.executeQuery(query);
			while(rs.next()) {
				BirthdayForm bObj=new BirthdayForm();
				bObj.setUserId(rs.getString(1));
				bObj.setUserName(rs.getString(3));
				bObj.setBirthDate(rs.getString(4));
				
				list.add(bObj);
			}

		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally 
		{
			/* close connection */
			try 
			{
				
				if (st != null) 
				{
					st.close();
				}
				if (rs != null) 
				{
					rs.close();
				}

			} catch (SQLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
				
		}
		System.out.println("List is.........................."+list);
		return list;
		
		
	}
	
	public ArrayList<BirthdayForm> showCurrentBirthdays() throws SQLException
	{
		String query = "SELECT U.*FROM Users U where month(SYSDATE()) =  month(U.birth_date) AND day(SYSDATE())= day(U.birth_date)";
		Statement st = null;
		
		ResultSet rs = null;
		
		ArrayList<BirthdayForm> list=new ArrayList<BirthdayForm>();
		try
		{
			
			st = con.createStatement();
			rs = st.executeQuery(query);
			while(rs.next()) {
				BirthdayForm bObj=new BirthdayForm();
				bObj.setUserId(rs.getString(1));
				bObj.setUserName(rs.getString(3));
				bObj.setBirthDate(rs.getString(4));
				list.add(bObj);
			}

		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally 
		{
			/* close connection */
			try 
			{
				
				if (st != null) 
				{
					st.close();
				}
				if (rs != null) 
				{
					rs.close();
				}

			} catch (SQLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
				
		}
		return list;
		
	}
	
	public void closeConnection()  throws SQLException
	{
		con.close();
		ConnectionFactory.closeConnection();
	}
		
}
